package striveen.util.used.loading;

import striveen.util.used.R;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class LoadingDataDialog extends Dialog {
	private Context context;
	private TextView tv_msg;
	private String st_loadingdata;

	public LoadingDataDialog(Context context) {
		super(context);
		this.context = context;
		init();
	}

	public LoadingDataDialog(Context context, boolean cancelable,
			DialogInterface.OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		this.context = context;
		init();
	}

	public LoadingDataDialog(Context context, int theme) {
		super(context, theme);
		this.context = context;
		init();
	}

	private void init() {
	}

	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(1);
		getWindow().setBackgroundDrawableResource(R.drawable.adddata_toast_bg);
		super.onCreate(savedInstanceState);
		setContentView(creatLayout());
	}

	private LinearLayout creatLayout() {
		LinearLayout layout = new LinearLayout(this.context);
		layout.setPadding(10, 10, 10, 10);
		layout.setGravity(16);

		ProgressBar progressBar = new ProgressBar(this.context, null, 16842873);
		layout.addView(progressBar);
		tv_msg = new TextView(context);
		if (TextUtils.isEmpty(st_loadingdata)) {
			tv_msg.setText(R.string.text_loadingdata);
		} else {
			tv_msg.setText(st_loadingdata);
			st_loadingdata = null;
		}
		tv_msg.setTextColor(Color.parseColor("#cccccc"));
		layout.addView(tv_msg);
		return layout;
	}

	public void setMessage(String msg) {
		if (null == tv_msg) {
			st_loadingdata = msg;
		} else {
			tv_msg.setText(msg);
		}

	}

	public void setMessage(int resId) {
		setMessage(context.getString(resId));
	}
}
